package com.moioo.exam;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;

/**
 * A data access object (DAO) providing persistence and search support for
 * TExamPaperUser entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.moioo.exam.TExamPaperUser
 * @author MyEclipse Persistence Tools
 */

public class TExamPaperUserDAO extends BaseHibernateDAO {
	private static final Log log = LogFactory.getLog(TExamPaperUserDAO.class);
	// property constants
	public static final String SCORE = "score";

	public void save(TExamPaperUser transientInstance) {
		log.debug("saving TExamPaperUser instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(TExamPaperUser persistentInstance) {
		log.debug("deleting TExamPaperUser instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public TExamPaperUser findById(com.moioo.exam.TExamPaperUserId id) {
		log.debug("getting TExamPaperUser instance with id: " + id);
		try {
			TExamPaperUser instance = (TExamPaperUser) getSession().get(
					"com.moioo.exam.TExamPaperUser", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(TExamPaperUser instance) {
		log.debug("finding TExamPaperUser instance by example");
		try {
			List results = getSession().createCriteria(
					"com.moioo.exam.TExamPaperUser").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding TExamPaperUser instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from TExamPaperUser as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByScore(Object score) {
		return findByProperty(SCORE, score);
	}

	public List findAll() {
		log.debug("finding all TExamPaperUser instances");
		try {
			String queryString = "from TExamPaperUser";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<TExamPaperUser> findByPaper(int id) {
		log.debug("finding all findByPaper instances");
		try {
			String queryString = "from TExamPaperUser as t where t.id.parperid = ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, id);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<TExamPaperUser> findByPaper(int id,int state) {
		log.debug("finding all findByPaper instances");
		try {
			String queryString = "from TExamPaperUser as t where t.id.parperid = ?  and t.state=?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, id);
			queryObject.setParameter(1, state);			
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public List<TExamPaperUser> findByUser(String id) {
		log.debug("finding all findByPaper instances");
		try {
			String queryString = "from TExamPaperUser as t where t.id.papssport = ? and state=1 order by t.examdate desc";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, id);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	public TExamPaperUser merge(TExamPaperUser detachedInstance) {
		log.debug("merging TExamPaperUser instance");
		try {
			TExamPaperUser result = (TExamPaperUser) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(TExamPaperUser instance) {
		log.debug("attaching dirty TExamPaperUser instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(TExamPaperUser instance) {
		log.debug("attaching clean TExamPaperUser instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}
}